package cn.tramy.pinxian.wind.domain.model

import java.time.LocalDateTime
import java.util.*
import javax.persistence.Id
import javax.persistence.MappedSuperclass

@MappedSuperclass
abstract class BaseDomainModel {
    @Id
    var uid: String = ""
        private set
    protected var createdAt: LocalDateTime = LocalDateTime.now()
    @Transient
    private var domainEvents: MutableList<Any>? = mutableListOf()

    protected constructor(uid: String, createdAt: LocalDateTime) {
        this.uid = uid
        this.createdAt = createdAt
    }

    protected fun apply(event: Any) = this.domainEvents?.add(event)

    fun domainEvents() = Collections.unmodifiableList(domainEvents ?: listOf())
}

@MappedSuperclass
abstract class BaseEnabledDomainModel(uid: String, enabled: Boolean, createdAt: LocalDateTime) : BaseDomainModel(uid, createdAt) {

    var enabled: Boolean = enabled
        private set

    fun baseEnable() {
        this.enabled = true
    }

    fun baseDisable() {
        this.enabled = false
    }
}


abstract class BaseDeletableDomainModel(uid: String, deleted: Boolean, createdAt: LocalDateTime) : BaseDomainModel(uid, createdAt) {
    var deleted: Boolean = deleted
        private set

    fun baseDelete() {
        if (!deleted) this.deleted = true
    }

}